<?php

namespace app\system\controller\logic;

use app\common\model\logic\CarBtypeModel;
use app\common\model\logic\CarOrderModel;
use app\common\model\WechatModel;
use think\Db;
use think\helper\Time;
use think\Request;

class StaticController extends \app\system\controller\BaseController
{

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    //车辆统计
    public function car(){
        $btime = $this->request->param('btime',0); //开始
        $etime = $this->request->param('etime',time()); //结束
        $result = Db::table('car_order')
            ->field("car_sn,".$this->getFields())
            ->where("car_status>1")
            ->whereTime('back_time','between',[$btime,$etime])
            ->group('car_sn')
            ->select();
        dump($result);
    }
    //客户欠款统计
    public function customer(){
        $btime = $this->request->param('btime',0); //开始
        $etime = $this->request->param('etime',time()); //结束
        $result = Db::table('car_order')
            ->field("btype_id,order_company,order_client,order_phone,".$this->getFields())
            ->where("car_status=3")
            ->whereTime('back_time','between',[$btime,$etime])
            ->group('btype_id')
            ->select();
        dump($result);
    }
    //通用字段
    private function getFields(){
        return "count(*) as count,sum(order_charge) as sum_order_charge,sum(order_advance) as sum_order_advance,
        sum(order_pay) as sum_order_pay,sum(discount_money) as sum_discount_money,sum(order_q) as sum_order_q";
    }


    public function test(){
        $btime = $this->request->param('btime',0); //开始
        $etime = $this->request->param('etime',time()); //结束
        $result = Db::table('customer_order')->alias('t1')
            ->join("financial t2","t1.customer_id=t2.customer_id",'LEFT')
            ->field("
t1.dept_id,
count(*),
sum(order_price),
sum(order_pay),
sum(order_refund)
")
            ->whereTime('financial_date','between',[$btime,$etime])
            ->group('t1.dept_id')
            ->select();
        dump($result);
    }

    public function loudou(){
        $ss['t1'] = Db::table('customer')->count();
        $ss['t2'] = Db::table('customer_order')->count();
        $ss['t3'] = Db::table('customer_order')->where('pay_status=1')->count();
        $ss['t4'] = Db::table('customer_order')->where('pay_status=2')->count();
        $ss['t5'] = Db::table('customer_order')->where('order_status=2')->count();
        $this->assign('sssss',$ss);
        return $this->fetch();
    }

    //资产统计
    public function asset(){
        $asset['t1'] = Db::table('car_order')->count();  //订单总数
        $asset['t2'] = Db::table('car_order')->count();   //订单总金额
        $asset['t3'] = Db::table('car_order')->count();  //代收款(有问题)
        $asset['t4'] = Db::table('car')->count();  //车辆总数
        $asset['t5'] = Db::table('car_driver')->count();  //司机总数
        $asset['t6'] = Db::table('user')->count();  //员工总数
        $this->assign('asset',$asset);
        return $this->fetch();
    }
    //车辆统计
    public function carsum(){
    	$rangdate=$this->request->param('rangdate','');
        $rangdatearr = explode('~',$rangdate);
        $begin = strtotime($rangdatearr[0].' 0:00:00');
        $end = strtotime($rangdatearr[1].' 23:59:59');
        $map['car_status']=['>',1];
        if(!empty($rangdate)){
            $map['back_time']=['between',[$begin,$end]];
        }
        $orderModel = new CarOrderModel();
        $list = $orderModel
            ->field('car_sn,aid,'.$this->getFields())
            ->where($map)
            ->group('car_sn')
            ->select(); //自己得收车订单
        //dump($list->toArray());
        if(!$list->isEmpty()){
            $list=$list->toArray();
            $car_sn = array_column($list,'car_sn');
            //$btypelist = cms_moudle(6)->where('aid','in',$car_sn)->column('*','aid');
            $sum=['sum_order_q'=>0,'sum_order_charge'=>0];
            foreach($list as $k=>$v){
                //$list[$k]['car_sn'] = $btypelist[$v['car_sn']];
                $sum['sum_order_q'] += $v['sum_order_q'];
                $sum['sum_order_charge'] += $v['sum_order_charge'];
            }
        }
        $this->assign('sum',$sum);
        $this->assign('rangdate',$rangdate);
        $this->assign('list',$list);
        return $this->fetch();

    }
    
    /**
     * 行程单付款
     */
    public function paymoney(){
        $rangdate=$this->request->param('rangdate','');
        $rangdatearr = explode('~',$rangdate);
        $begin = strtotime($rangdatearr[0].' 0:00:00');
        $end = strtotime($rangdatearr[1].' 23:59:59');
        $map['car_status']=['>',1];
        $map['btype_aid']=1;
        $map['order_q']=['>',0];
        if(!empty($rangdate)){
            $map['back_time']=['between',[$begin,$end]];
        }
        $orderModel = new CarOrderModel();
        $list = $orderModel
            ->field('btype_id,sum(order_q) as sum_order_q,sum(order_charge) as sum_order_charge')
            ->where($map)
            ->group('btype_id')
            ->select(); //自己得收车订单
        if(!$list->isEmpty()){
            $list=$list->toArray();
            $btype_ids = array_column($list,'btype_id');
            $btypelist = cms_moudle(6)->where('aid','in',$btype_ids)->column('*','aid');
            $sum=['sum_order_q'=>0,'sum_order_charge'=>0];
            foreach($list as $k=>$v){
                $list[$k]['btype'] = $btypelist[$v['btype_id']];
                $sum['sum_order_q'] += $v['sum_order_q'];
                $sum['sum_order_charge'] += $v['sum_order_charge'];
            }
        }
        $this->assign('sum',$sum);
        $this->assign('rangdate',$rangdate);
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    //订单每月新建数量
    public function ordernew(){
        $result = Db::table('car_order')
            ->field("FROM_UNIXTIME(order_start_time,'%Y-%m') as months,count(aid) as sum")
            ->group('months')
            ->select();
        $this->assign('result',$result);
        return $this->fetch();
    }
}
